﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using Fayshine.VA.RIAService.Web;
using Fayshine.VA.ViewModels;
using LocalBusyIndicator = Fayshine.VA.Controls.BusyIndicator;
using Fayshine.VA.Helpers;

namespace Fayshine.VA.Views.SearchStatistics
{
    /// <summary>
    /// 任意时段汇总统计
    /// </summary>
    public partial class AnyDurationStatistics : Page
    {
        private PassengerFlowViewModel m_ViewModel;

        public AnyDurationStatistics()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(AnyDurationStatistics_Loaded);
        }

        private void AnyDurationStatistics_Loaded(object sender, RoutedEventArgs e)
        {
            LocalBusyIndicator indicator = Application.Current.RootVisual as LocalBusyIndicator;
            indicator.IsBusy = true;

            this.startDatePicker.SelectedDate = DateTime.Now;
            this.endDatePicker.SelectedDate = DateTime.Now;
            this.startTimePicker.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 8, 0, 0);
            this.endTimePicker.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 21, 0, 0);

            m_ViewModel = new PassengerFlowViewModel();
            this.DataContext = m_ViewModel;

            m_ViewModel.GetAllAreas();
        }

        private void cmbAreas_DropDownOpened(object sender, EventArgs e)
        {

        }

        private void cmbAreas_DropDownClosed(object sender, EventArgs e)
        {
            if (m_ViewModel != null)
            {
                List<area> selectedArea = new List<area>();
                var selectAreaExt = m_ViewModel.AreaExtends.Where(a => a.IsSelected).ToList();
                selectAreaExt.ForEach(b =>
                {
                    area temp = b.Tag as area;
                    selectedArea.Add(temp);
                });

                if (selectedArea.Count > 0)
                {
                    cbItemDisplay.Content = selectedArea.Count > 1 ? selectedArea[0].Name + "..." : selectedArea[0].Name;
                }
                else
                {
                    cbItemDisplay.Content = string.Empty;
                }
                cmbAreas.SelectedItem = null;
                cmbAreas.SelectedItem = cbItemDisplay;
                m_ViewModel.SelectAreasChanged(selectedArea,true);
            }
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            if (!m_ViewModel.IsConditionSelected())
            {
                MessageBox.Show("请选择店铺或者门面！");
                return;
            }

            if (startDatePicker.SelectedDate == null || endDatePicker.SelectedDate == null ||
               startTimePicker.Value == null || endTimePicker.Value == null)
            {
                MessageBox.Show("请选择正确的日期和时间");
            }
            else
            {
                DateTime startTime = new DateTime(startDatePicker.SelectedDate.Value.Year,
                                                  startDatePicker.SelectedDate.Value.Month,
                                                  startDatePicker.SelectedDate.Value.Day,
                                                  startTimePicker.Value.Value.Hour,
                                                  startTimePicker.Value.Value.Minute,
                                                  startTimePicker.Value.Value.Second);
                DateTime endTime = new DateTime(endDatePicker.SelectedDate.Value.Year,
                                                 endDatePicker.SelectedDate.Value.Month,
                                                 endDatePicker.SelectedDate.Value.Day,
                                                 endTimePicker.Value.Value.Hour,
                                                 endTimePicker.Value.Value.Minute,
                                                 endTimePicker.Value.Value.Second);

                if (startTime > endTime)
                {
                    MessageBox.Show("起始时间不能大于结束时间");
                }
                else if (cmbAreas.SelectedIndex < 0 || cmbInorOut.SelectedIndex < 0)
                {
                    MessageBox.Show("请选择门店和统计方式");
                }
                else
                {
                    LocalBusyIndicator indicator = Application.Current.RootVisual as LocalBusyIndicator;
                    indicator.IsBusy = true;
                    bool isIn = true;
                    isIn = cmbInorOut.SelectedIndex == 0 ? true : false;
                    m_ViewModel.QueryPassengerFlowByTime(isIn, startTime, endTime);
                }
            }
        }

        private void btnExport_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog fsd = new SaveFileDialog();
            fsd.Filter = "PDF (*.pdf)|*.pdf|*PNG (*.png)|*.png";

            if (fsd.ShowDialog() == true)
            {
                LocalBusyIndicator indicator = Application.Current.RootVisual as LocalBusyIndicator;
                indicator.IsBusy = true;
                this.Dispatcher.BeginInvoke(delegate()
                {
                    FayshineManager.Instance.ExportToDisk(fsd, spContent);
                });
            }

        }

    }
}
